/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "ListMiiInfos.h"
//#include "../../FileOperations/fileops.h"
//#include "../../FileOperations/Nand.hpp"
#include "../../Themes/CTheme.h"
#include "../../TextOperations/FreeTypeGX.h"
#include "../../Miis/MiiTools.h"
//#include "../../Saves/DataBin/DataBinTools.h"
//#include "../../Saves/DataBin/Crypto/aes.h"
//#include "../../Gui/gui_banner.h"

MiiInfosWindow::MiiInfosWindow(Browser * b, int w, int h)
	: GuiFrame(w, h)
{
	browser = b;
	browser->IndexChanged.connect(this, &MiiInfosWindow::OnBrowserIndexChange);
	
	selIndex = -1;
	
	fileMii = Resources::GetImageData("icon_brows_mii.png");
	maleData = Resources::GetImageData("icon_mii_male.png");
	femaleData = Resources::GetImageData("icon_mii_female.png");
	
	
	iconImg = new GuiImage(fileMii);
	iconImg->SetScale(1.5);
	
	nameTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	nameTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	nameTxt->SetMaxWidth(width, SCROLL_HORIZONTAL);
	
	creatorTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	creatorTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	creatorTxt->SetMaxWidth(width, DOTTED);
	
	dateTxt = new GuiText((char*) NULL, 16, thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	dateTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	dateTxt->SetMaxWidth(width, DOTTED);
	
	genderImg = NULL;
	colorImg = NULL;
	
	path = new GuiText(tr( "Path:" ), thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	path->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	path->SetPosition(0, -57);
	
	pathTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	pathTxt->SetLinesToDraw(3);
	pathTxt->SetInterline(3);
	pathTxt->SetMaxWidth(width, WRAP);
	pathTxt->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	pathTxt->SetPosition(0, -38);
}

MiiInfosWindow::~MiiInfosWindow()
{
	RemoveAll();
	
	delete iconImg;
	delete nameTxt;
	delete creatorTxt;
	delete dateTxt;
	delete genderImg;
	delete colorImg;
	delete path;
	delete pathTxt;
	
	Resources::Remove(fileMii);
	Resources::Remove(maleData);
	Resources::Remove(femaleData);
}

void MiiInfosWindow::OnBrowserIndexChange()
{
	SetInfos();
}

void MiiInfosWindow::SetInfos()
{
	RemoveAll();
	
	if(genderImg)
		delete genderImg;
	if(colorImg)
		delete colorImg;
	genderImg = NULL;
	colorImg = NULL;
	
	if(!browser->GetEntrieCount())
		return;
	
	selIndex = browser->GetSelIndex();
	
	int y = iconImg->GetHeight()+9;
	nameTxt->SetText(browser->GetMiiName(selIndex));
	nameTxt->SetPosition(0, y);
	y += 19;
	
	std::wstring creator = charToWideChar(tr( "Creator:" ));
	creator += L" ";
	if(!browser->GetMiiCreator(selIndex).empty())
		creator += browser->GetMiiCreator(selIndex);
	else
		creator += L"...";
	
	creatorTxt->SetText(creator);
	creatorTxt->SetPosition(0, y);
	y += 25;
	
	GetDate();
	dateTxt->SetPosition(0, y);
	y += 25;
	
	genderImg = new GuiImage(browser->GetMiiFemale(selIndex) ? femaleData : maleData);
	genderImg->SetAlignment(ALIGN_CENTER | ALIGN_TOP);
	genderImg->SetPosition(-width/4, y);	
	
	colorImg = new GuiImage(24, 24, GetFavColor());
	colorImg->SetAlignment(ALIGN_CENTER | ALIGN_TOP);
	colorImg->SetPosition(width/4, y);
	
	std::string miiPath = fmt("isfs:%s", MII_FILE);
	if(Settings.LoadedBrowser == EMII_BROWSER)
		miiPath = fmt("%s%s", browser->GetCurrentPath().c_str(), MII_FILE);
	pathTxt->SetText(miiPath);
	
	Append(iconImg);
	Append(nameTxt);
	Append(creatorTxt);
	Append(dateTxt);
	Append(genderImg);
	Append(colorImg);
	Append(path);
	Append(pathTxt);
}

void MiiInfosWindow::GetDate()
{
	std::string date = tr("Date:");
	date += " ";
	
	std::string tmpDate =  "..";
	
	if (browser->GetMiiDay(selIndex) != 0)
		tmpDate = fmt("%i", browser->GetMiiDay(selIndex));
	tmpDate += " / ";
	
	switch (browser->GetMiiMonth(selIndex))
	{
		case 0: { tmpDate += ".."; break; }
		case 1: { tmpDate += tr("January"); break; }
		case 2: { tmpDate += tr("February"); break; }
		case 3: { tmpDate += tr("March"); break; }
		case 4: { tmpDate += tr("April"); break; }
		case 5: { tmpDate += tr("May"); break; }
		case 6: { tmpDate += tr("June"); break; }
		case 7: { tmpDate += tr("July"); break; }
		case 8: { tmpDate += tr("August"); break; }
		case 9: { tmpDate += tr("September"); break; }
		case 10: { tmpDate += tr("October"); break; }
		case 11: { tmpDate += tr("November"); break; }
		case 12: { tmpDate += tr("December"); break; }
	}
	
	date += tmpDate;
	
	dateTxt->SetText(date);
}

GXColor MiiInfosWindow::GetFavColor()
{
	GXColor color = (GXColor) {0, 0, 0, 0};
	
	switch (browser->GetMiiFavColor(selIndex))
	{
		case 0: { color = (GXColor) {255, 0, 0, 255}; break; }
		case 1: { color = (GXColor) {255, 150, 0, 255}; break; }
		case 2: { color = (GXColor) {255, 255, 0, 255}; break; }
		case 3: { color = (GXColor) {140, 255, 0, 255}; break; }
		case 4: { color = (GXColor) {0, 130, 0, 255}; break; }
		case 5: { color = (GXColor) {0, 100, 255, 255}; break; }
		case 6: { color = (GXColor) {0, 200, 255, 255}; break; }
		case 7: { color = (GXColor) {255, 50, 140, 255}; break; }
		case 8: { color = (GXColor) {190, 0, 255, 255}; break; }
		case 9: { color = (GXColor) {140, 80, 45, 255}; break; }
		case 10: { color = (GXColor) {255, 255, 255, 255}; break; }
		case 11: { color = (GXColor) {0, 0, 0, 255}; break; }
	}
	
	return color;
}
